package TruongNhaLib;

import www.truongnha.com.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;

public class TriToggleButton extends Button
{
	private String DEBUGTAG = "CustonButtonExample";
	
	// Keeps track of the current state, 0, 1, or 2
	private int _state;

	// Get the attributes created in attrs.xml
	private static final int[] STATE_ONE_SET =
	{
		R.attr.state_one
	};
	
	private static final int[] STATE_TWO_SET =
	{
		R.attr.state_two
	};
	
	private static final int[] STATE_THREE_SET =
	{
		R.attr.state_three
	};
	private static final int[] STATE_FOUR_SET =
	{
		R.attr.state_four
	};

	// Constructors
	public TriToggleButton(Context context)
	{
		super(context);
		
		// Set the default state and text
		_state = 3;
		this.setTextColor(Color.GRAY);
		this.setText("Đi học");
		Resources res = getResources();
		Drawable shape = res.getDrawable(R.drawable.tri_button_selector);
		this.setBackgroundDrawable(shape);
	}
	
	public TriToggleButton(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		
		// Set the default state and text
		_state = 3;
		this.setTextColor(Color.GRAY);
		this.setText("Đi học");
		Resources res = getResources();
		Drawable shape = res.getDrawable(R.drawable.tri_button_selector);
		this.setBackgroundDrawable(shape);
	}
	
	public TriToggleButton(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		
		// Set the default state and text
		_state = 3;
		this.setTextColor(Color.GRAY);
		this.setText("Đi học");
		Resources res = getResources();
		Drawable shape = res.getDrawable(R.drawable.tri_button_selector);
		this.setBackgroundDrawable(shape);
		
	}
	
	@Override
	public boolean performClick()
	{
		// Move to the next state
		nextState();
		
		return super.performClick();
	}
	
	// Generate the drawable needed for the current state
	@Override
	protected int[] onCreateDrawableState(int extraSpace)
	{
		// Add the number of states you have
		final int[] drawableState = super.onCreateDrawableState(extraSpace + 3);
		
		if(_state == 0)
		{
			mergeDrawableStates(drawableState, STATE_ONE_SET);
		}
		else if(_state == 1)
		{
			mergeDrawableStates(drawableState, STATE_TWO_SET);
		}
		else if(_state == 2)
		{
			mergeDrawableStates(drawableState, STATE_THREE_SET);
		}
		else if(_state == 3)
		{
			mergeDrawableStates(drawableState, STATE_FOUR_SET);
		}
		
		return drawableState;
	}
	
	// Set current state, 0-3
	public void setState(int state)
	{
		if((state > -1) && (state < 4))
		{
//			Log.d(DEBUGTAG, "   Setting Toggle state to " + state);
			_state = state;
			setButtonText();
		}
	}
	
	// Returns current state
	public int getState()
	{
		return _state;
	}

	// Increases state, or loops to 0
	public void nextState()
	{
		_state++;
		
		// Loop around if at the last state
		if(_state > 3)
		{
			_state = 0;
		}
		setButtonText();
//		Log.d(DEBUGTAG, "   Setting Toggle state to " + _state);
//		showShortToast("ToggleState: " + _state);
	}
	
	// Decreases state, or loops to 2
	public void previousState()
	{
		_state--;
		
		// Loop around if at the first state
		if(_state < 0)
		{
			_state = 3;
		}
		
		setButtonText();
		Log.d(DEBUGTAG, "   Setting Toggle state to " + _state);
//		showShortToast("ToggleState: " + _state);
	}
	
	// Set the text displayed on the button
	private void setButtonText()
	{
		Log.d(DEBUGTAG, "   Set Button text " + _state);
		switch(_state)
		{
			case 0: {
					this.setTextColor(Color.rgb(204, 0, 0));
					this.setText("Ko phép");
					break;}
			case 1: {
					this.setTextColor(Color.rgb(70, 136, 71));
					this.setText("Có phép");
					break;}
			case 2: {
					this.setTextColor(Color.rgb(58, 135, 173));
					this.setText("Đi muộn");
					break;}
			case 3:{
					this.setTextColor(Color.GRAY);
					this.setText("Đi học");
					break;}
			default: this.setText("N/A"); // Should never happen, but just in case
					break;
		}
	}
	
}
